<?php


namespace app\unify\model;


use think\db\Query;
use think\Model;
use app\unify\model\User as UserModel;
use app\unify\model\Role as RoleModel;

class Auth extends Base
{
    public static function onBeforeDelete(Model $model): bool
    {
        $id = $model->getData($model->getPk());

        if (!Role::hasWhere('authIds', function (Query $query) use ($id) {
            $query->where('auth_id', '=', $id);
        })->select()->isEmpty()) {
            throw new \Exception('删除失败，当前存在角色关联该权限，请先解绑。');
        }

        if (!Menu::hasWhere('authIds', function (Query $query) use ($id) {
            $query->where('auth_id', '=', $id);
        })->select()->isEmpty()) {
            throw new \Exception('删除失败，当前存在菜单关联该权限，请先解绑。');
        }

        return true;
    }

}